/*-+-*****************************************************************************
 Copyright (C) 2009-2012 Oceanside Software Corporation (R)
 Dallas, Texas

 Author       : Jason T. Brower
 Purpose      : This file is part of a point-of-sale software package
                originally written for Table-Service Restaurants.
              
 Date         : July 8th of 2012
              
 History      : The application that this software file belongs to was 
                written over a 3-Year period.  The first version was written
                in C# .Net 3.5 using Windows Forms.  Being new to .Net at
                the time, I soon learned of Windows Presentation Foundation,
                liked the graphics capabilities and databinding so I
                migrated to WPF.  When the .Net 4.0 framework was released
                the application was migrated from 3.5 to 4.0. Now today,
                .Net 4.5 RC is out and I was already in process of migrating
                before I made this public.
                
Excuse Me     : Please excuse the coding style, it is not representative of
                my typical work.  During the three years I was under a very
                tight budget, constantly running out of funding and was 
                working 18 hour days hyped up on 5-Hour Energy.  The 
                design is not textbook.  It is monolythic because of
                previous issues I will not get into.  There are few comments
                in the code and the ones that are there may be incorrect.
                
What it is not: This is not the exact version that I took through PA-DSS 2.0
                nor is it the exact version running at my customer sites.
                I removed some details for security purposes including some
                encryption algorithms.  The reporting was removed because
                I was including a third party library that cannot be 
                published with this Open Source.
                
What it is    : This application has been running at high volume restaraunts
                for over a year.  It never freezes, crashes or does anything
                unexpected.  So long as it is built, installed and configured
                correctly, it is a strong system that we can all enhance.

Contact       : Please note that I can no longer work for free.  However, you
                can reach me at information@oceansidesw.com.                                                      

*********************************************************************************/
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data.Objects.DataClasses;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
#if _LOCALDB
  using Oceanside.LocalDB;
#else
using Oceanside.RemoteDB;
#endif
namespace Oceanside
{
    /// <summary>
    /// 
    /// </summary>
    public partial class PageRecipesImp : PageImpBaseClass
    {
        ObservableCollection<PRODUCT> availableProdsObsCollection;
        ObservableCollection<PRODUCT> usedProdsObsCollection;

        ListView listviewChosenMods;
        ListView listviewAvailableMods;
        ComboBox comboBoxName;
        Button buttonAddSelectedMods;
        Button buttonRemoveSelectedMods;

        /// <summary>
        /// Constructor
        /// </summary>
        public PageRecipesImp(UIElementCollection stackControlsColl, ListView listviewChosenMods, ListView listviewAvailableMods, ComboBox comboBoxName, Button buttonAddSelectedMods, Button buttonRemoveSelectedMods)
            : base()
        {
            this.ButtonNumber = "24";
            this.ButtonLabelText = "Simple Recipes";

            this.listviewChosenMods = listviewChosenMods;
            this.listviewAvailableMods = listviewAvailableMods;
            this.comboBoxName = comboBoxName;
            this.buttonAddSelectedMods = buttonAddSelectedMods;
            this.buttonRemoveSelectedMods = buttonRemoveSelectedMods;

            this.buttonAddSelectedMods.Click += new RoutedEventHandler(buttonAddSelectedMods_Click);
            this.buttonRemoveSelectedMods.Click += new RoutedEventHandler(buttonRemSelectedMods_Click);

            ///Register the controls and lock the form.
            RegisterUiElementsList(stackControlsColl);

            availableProdsObsCollection = new ObservableCollection<PRODUCT>();
            usedProdsObsCollection = new ObservableCollection<PRODUCT>();

            listviewChosenMods.ItemsSource = usedProdsObsCollection;
            listviewAvailableMods.ItemsSource = availableProdsObsCollection;
            RegisterListview(listviewAvailableMods);
            RegisterListview(listviewChosenMods);

            mListOfControlsToHideInNonEditMode.Add(buttonAddSelectedMods);
            mListOfControlsToHideInNonEditMode.Add(buttonRemoveSelectedMods);

            ///If the selection changes lock the form down.
            comboBoxName.SelectionChanged += new SelectionChangedEventHandler(comboBoxName_SelectionChanged_NOTHROW);


            ///Add the combo boxes.
            ListofComboboxes.Add(comboBoxName);

            listviewAvailableMods.SelectionChanged += new SelectionChangedEventHandler(listviewChosenMods_SelectionChanged_NOTHROW);
            listviewChosenMods.SelectionChanged += new SelectionChangedEventHandler(listviewChosenMods_SelectionChanged_NOTHROW);


            mEntityAddDeleteLoggingEnabled = true;

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void listviewChosenMods_SelectionChanged_NOTHROW(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                if (this.IsEditableMode())
                {
                    ListView lv = sender as ListView;

                    if (lv == listviewAvailableMods)
                    {
                        if (lv.SelectedItems.Count != 0)
                        {
                            buttonAddSelectedMods.IsEnabled = true;
                        }
                        else
                        {
                            buttonAddSelectedMods.IsEnabled = false;
                        }
                    }
                    else if (lv == listviewChosenMods)
                    {
                        if (lv.SelectedItems.Count != 0)
                        {
                            buttonRemoveSelectedMods.IsEnabled = true;
                        }
                        else
                        {
                            buttonRemoveSelectedMods.IsEnabled = false;
                        }
                    }
                }
            }
            catch (System.Exception exc)
            {
                Constants.ErrorDispatcher.Notify(Error.EType.E_PAGEEXMODGR_LISTVIEW_SELECTION_CHANGE, exc);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public override void ResetFocus()
        {
            base.ResetFocus();
            buttonAddSelectedMods.IsEnabled = false;
            buttonRemoveSelectedMods.IsEnabled = false;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        override public List<EntityObject> GetEntityObjectSet()
        {
            return new List<EntityObject>(Context.EXCEPTION_MODIFIER_GROUPS.ToList());
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void comboBoxName_SelectionChanged_NOTHROW(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                PRODUCT group = (sender as ComboBox).SelectedItem as PRODUCT;
                //if (group != null)
                //{
                //    group.RECIPE_INGREDIENTS.Load();
                //    DominicaEntities.GetUnusedProductObsvColl(group.RECIPE_INGREDIENTS, availableProdsObsCollection, Context, true);
                //    DominicaEntities.AlphaOrderMyProducts(group.RECIPE_INGREDIENTS, usedProdsObsCollection);
                //}
            }
            catch (System.Exception exc)
            {
                Constants.ErrorDispatcher.Notify(Error.EType.E_PAGEEXMODGR_COMBOBOX_NAME_SELECTION_CHANGE, exc);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override EntityObject GetSelectedEntityObject()
        {
            PRODUCT selected = comboBoxName.SelectedItem as PRODUCT;
            return selected;
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="source"></param>
        public override void UpdateUsersEntitySource()
        {
            ///Create the query object.
            var query = from PRODUCT in Context.PRODUCTS where PRODUCT.IsDeleted == false orderby PRODUCT.Name select PRODUCT;

            if (query.Count() != 0)
            {
                //List<PRODUCT> list = query.ToList();
                //comboBoxName.ItemsSource = list;
                //PRODUCT group = list.First();
                //comboBoxName.SelectedItem = group;

                //if (group != null)
                //{
                //    group.RECIPE_INGREDIENTS.Load();
                //    DominicaEntities.GetUnusedProductObsvColl(group.RECIPE_INGREDIENTS, availableProdsObsCollection, Context, true);
                //    DominicaEntities.AlphaOrderMyProducts(group.RECIPE_INGREDIENTS, usedProdsObsCollection);
                //    listviewChosenMods.ItemsSource = usedProdsObsCollection;
                //    listviewAvailableMods.ItemsSource = availableProdsObsCollection;
                //}
                //else
                //{
                //    listviewChosenMods.ItemsSource = null;
                //    listviewAvailableMods.ItemsSource = null;
                //}
            }
            else
            {
                listviewChosenMods.ItemsSource = null;
                listviewAvailableMods.ItemsSource = null;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override ComboBox GetEntityComboBox()
        {
            return comboBoxName;
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonAddSelectedMods_Click(object sender, RoutedEventArgs e)
        {
            PRODUCT modGroup = comboBoxName.SelectedItem as PRODUCT;
            if (modGroup != null)
            {
                //foreach (object prodMod in listviewAvailableMods.SelectedItems)
                //{
                //    modGroup.RECIPE_INGREDIENTS.Add(prodMod as PRODUCT);
                //}

                //DominicaEntities.GetUnusedProductObsvColl(modGroup.RECIPE_INGREDIENTS, availableProdsObsCollection, Context, true);
                //DominicaEntities.AlphaOrderMyProducts(modGroup.RECIPE_INGREDIENTS, usedProdsObsCollection);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonRemSelectedMods_Click(object sender, RoutedEventArgs e)
        {
            PRODUCT modGroup = comboBoxName.SelectedItem as PRODUCT;
            if (modGroup != null)
            {
                //foreach (object prodMod in listviewChosenMods.SelectedItems)
                //{
                //    modGroup.RECIPE_INGREDIENTS.Remove(prodMod as PRODUCT);
                //}

                //DominicaEntities.GetUnusedProductObsvColl(modGroup.RECIPE_INGREDIENTS, availableProdsObsCollection, Context, true);
                //DominicaEntities.AlphaOrderMyProducts(modGroup.RECIPE_INGREDIENTS, usedProdsObsCollection);
            }
        }
    }
}
